package com.Controller;

import com.Service.CategoryService;
import com.Service.GroupActivityService;
import com.Service.GroupJoinService;
import com.Service.ProductService;
import com.pojo.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/front")
public class FrontProductController {
    @Resource
    private CategoryService categoryService;

    @Resource
    private ProductService productService;

    @Resource
    private GroupActivityService groupActivityService;

    @Resource
    private GroupJoinService groupJoinService;

    @GetMapping("/products")
    public String listProducts(@RequestParam(value = "categoryId", required = false) Long categoryId, Model model) {
        List<Category> categoryList = categoryService.findAll();
        List<Product> productList = (categoryId == null)
                ? productService.findAll()
                : productService.findByCategoryId(categoryId);

        model.addAttribute("categoryList", categoryList);
        model.addAttribute("productList", productList);
        return "front/product_list";
    }

    @GetMapping("/product/detail")
    public String detail(@RequestParam("id") Long id, Model model) {
        Product product = productService.findById(id);
        model.addAttribute("product", product);
        System.out.println(product);
        GroupActivity activity = groupActivityService.findActiveByProductId(id);
        model.addAttribute("groupActivity", activity);
        System.out.println(activity);
        // 新增：查询该商品对应拼团活动下所有未满员的团
        if (activity != null) {
            List<GroupJoin> openGroups = groupJoinService.findOpenGroupsByActivityId(activity.getId());
            model.addAttribute("existingGroups", openGroups);
            System.out.println(openGroups);
        }

        return "front/product_detail";
    }
    @PostMapping("/group/init")
    public String initiateGroup(@RequestParam(required = false) Long activityId, @RequestParam(required = false) Long productId, HttpSession session, RedirectAttributes redirectAttributes) {
        User user=(User) session.getAttribute("user");
        if (user.getId() == null) {
            redirectAttributes.addFlashAttribute("error", "请先登录后再操作拼团。");
            return "redirect:/front/login";
        }

        // 如果传的是 activityId，表示加入已有拼团
        if (activityId != null) {
            groupJoinService.join(activityId, user.getId());
            return "redirect:/front/group/detail?id=" + activityId;
        }

        // 如果传的是 productId，说明要新建一个拼团活动
        if (productId != null) {
            Long newActivityId = groupActivityService.initiateGroup(productId, user.getId());
            return "redirect:/front/group/detail?id=" + newActivityId;
        }

        // 两个都没有，返回错误
        redirectAttributes.addFlashAttribute("error", "缺少拼团参数");
        return "redirect:/front/products";
    }

}
